package jp.seraph.same.view;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.math.geometry.Vector3D;

import jp.seraph.jsade.math.Point3D;
import jp.seraph.same.model.AgentModel;

public class AgentCanvas extends Canvas {
    public AgentCanvas(AgentModel aModel) {
        mModel = aModel;

        mTimer = new Timer();
        mTimer.schedule(new Task(this), 5000, 2000);
    }

    private class Task extends TimerTask {
        public Task(AgentCanvas aCanvas) {
            mCanvas = aCanvas;
        }

        private AgentCanvas mCanvas;

        public void run() {
            mCanvas.repaint();
        }
    }

    private AgentModel mModel;
    private Timer mTimer;
    private static final double SCALE_FACTOR = 10;
    private static final long serialVersionUID = 10L;

    /**
     * 
     * @see java.awt.Canvas#paint(java.awt.Graphics)
     */

    public void paint(Graphics aGraphics) {
        Vector3D tBall = mModel.getBallVector().scalarMultiply(SCALE_FACTOR);
        Vector3D tF1L = mModel.getLeftField1().scalarMultiply(SCALE_FACTOR);
        Vector3D tF2L = mModel.getLeftField2().scalarMultiply(SCALE_FACTOR);
        Vector3D tF1R = mModel.getRightField1().scalarMultiply(SCALE_FACTOR);
        Vector3D tF2R = mModel.getRightField2().scalarMultiply(SCALE_FACTOR);
        Vector3D tUnitX = mModel.getUnitX().scalarMultiply(20);
        Vector3D tUnitY = mModel.getUnitY().scalarMultiply(20);
        if (tBall == null)
            return;

        paintPoint(aGraphics, tBall, Color.RED, 5);
        paintPoint(aGraphics, Vector3D.zero, Color.BLACK, 8);

        paintPoint(aGraphics, tF1L, Color.GREEN, 5);
        paintPoint(aGraphics, tF2L, Color.YELLOW, 5);
        paintPoint(aGraphics, tF1R, Color.BLUE, 5);
        paintPoint(aGraphics, tF2R, Color.ORANGE, 5);

        paintArrow(aGraphics, tUnitX, Color.BLUE);
        paintArrow(aGraphics, tUnitY, Color.RED);
    }

    private void paintPoint(Graphics aGraphics, Vector3D aVector, Color aColor, int aSize) {
        Point3D tPoint = this.getCenter().add(rightHandToLeftHand(aVector));
        aGraphics.setColor(aColor);
        aGraphics.fillOval((int) tPoint.getX(), (int) tPoint.getY(), aSize, aSize);
        // aGraphics.drawString(aPoint.toString(1), (int)tPoint.getX(),
        // (int)tPoint.getY());
    }

    private void paintArrow(Graphics aGraphics, Vector3D aVector, Color aColor) {
        Point3D tCenter = this.getCenter();
        Point3D tTarget = tCenter.add(rightHandToLeftHand(aVector));

        aGraphics.setColor(aColor);
        aGraphics.drawLine((int) tCenter.getX(), (int) tCenter.getY(), (int) tTarget.getX(), (int) tTarget.getY());
    }

    private Vector3D rightHandToLeftHand(Vector3D aV) {
        return new Vector3D(aV.getX(), -aV.getY(), aV.getZ());
    }

    public Point3D getCenter() {
        int tX = this.getWidth() / 2;
        int tY = this.getHeight() / 2;

        return new Point3D(tX, tY, 0);
    }
}
